package com.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.common.uilt.BaseAction;
import com.google.gson.Gson;
import com.model.AssetsClassBean;
import com.model.DeptBean;
import com.model.Messages;
import com.service.imp.MenuService;
import com.service.imp.StaffService;
import com.service.imp.TreeService;
@Controller
@ParentPackage("defaultpackage")
@Namespace("/back")
@Action(value = "tree", results = {
		@Result(name = "assets", location = "/back/basis/assetsclass.jsp"),
		@Result(name = "dept", location = "/back/basis/dept.jsp"),
		@Result(name = "error", location = "/hello.jsp"), })
public class TreeAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5748626303065470526L;
	@Autowired
	private TreeService treeService;
	@Autowired
	private StaffService staffService;
	@Autowired
	private MenuService menuService;
	private String id;
	private AssetsClassBean ac;
	private DeptBean dept;
	/**
	 * 获取角色的权限菜单
	 * @return
	 * @throws Exception
	 */
	public String loadUserMenu() throws Exception{
		PrintWriter out = null;
		try {
			out = response.getWriter();
			List<Map<String, Object>> menuTreeList = this.menuService
					.loadUserMenuTree(session);
			Gson gson = new Gson();
			String jsonStr = gson.toJson(menuTreeList);
			out.print(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 跳转部门树页面
	 * @return
	 */
	public String dept(){
		return "dept";
	}
	/**
	 * 获取全部树 
	 * 资产分类+部门
	 * +存放地点
	 * +
	 * @return
	 */
	public String getAllTree() throws Exception{
		PrintWriter out = response.getWriter();
		try {
			Gson gson = new Gson();
			List<Map<String,Object>> l=this.treeService.getALLTree();
			String jsonStr = gson.toJson(l);
			out.print(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 跳转资产分类管理页面
	 * @return
	 */
	public String assetsclass(){
		//request.setAttribute("assetsTreeData", this.treeService.getAssetsTree());
		return "assets";
	}
	/**
	 * 获取部门树
	 * @return
	 * @throws Exception
	 */
	public String deptTree() throws Exception{
		PrintWriter out = response.getWriter();
		try {
			Gson gson = new Gson();
			List<Map<String,Object>> l=this.treeService.getDeptTree();
			String jsonStr = gson.toJson(l);
			out.print(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 获取资产分类树
	 * @return
	 * @throws Exception
	 */
	public String assetsclasstree() throws Exception{
		//System.out.println(id);
		PrintWriter out = response.getWriter();
		try {
			Gson gson = new Gson();
			List<Map<String,Object>> l=this.treeService.getAssetsclassTree();
			String jsonStr = gson.toJson(l);
			out.print(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.flush();
		out.close();
		return NONE;
	}
	public String addAssetsclass() throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m=new Messages();
		try {
			m= this.treeService.addAssetsclass(ac);
		}catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String editAssetsclass() throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m=new Messages();
		try {
			m= this.treeService.editAssetsclass(ac);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	public String delAssetsclass() throws Exception{
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		Messages m=new Messages();
		try {
			m= this.treeService.delAssetsclass(id);
		} catch (Exception e) {
			e.printStackTrace();
			m.setInfo(false);
			m.setText(e.getMessage());
		}
		out.print(gson.toJson(m));
		out.flush();
		out.close();
		return NONE;
	}
	/**
	 * 获取部门折旧报表
	 * @return
	 */
	public String getdept_dTree() throws Exception{
		PrintWriter out = response.getWriter();
		try {
			//Gson gson = new Gson();
			//List<Map<String,Object>> l=this.treeService.getAssetsclassTree();
			//String jsonStr = gson.toJson(l);
			//out.print(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.flush();
		out.close();
		return NONE;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public AssetsClassBean getAc() {
		return ac;
	}
	public void setAc(AssetsClassBean ac) {
		this.ac = ac;
	}
	public DeptBean getDept() {
		return dept;
	}
	public void setDept(DeptBean dept) {
		this.dept = dept;
	}

	
}
